<script>
	import {
		checkRegisterAPI,
		getTokenAPI,
		refreshTokenFun
	} from "@/api/api/user"
	import {
		baseUrl
	} from '@/api/http/setting'
	import {
		bluetoothStart,
		closeBLEConnection
	} from "@/static/bluetooth/core.js"
	import loginMixins from "@/pages/loginMixins.js"
	import {client} from "@/utils/utils.js"

	export default {
		mixins: [loginMixins],
		//小程序启动的时候
		onLaunch: function(option) {
			console.log("onLaunch")
			//#ifdef MP-WEIXIN
			if(baseUrl.indexOf("https://car.cq-ct.com") == -1){
				uni.setEnableDebug({
					enableDebug: true
				})
				this.$store.commit("setIsProduct",false);
			}else{
				this.$store.commit("setIsProduct",true);
			}
		    //#endif
			// if (uni.getSystemInfoSync().platform !== "devtools") {
			// 	console.log = () => {};
			// }
		},
		onShow: function() {
			let  option = uni.getLaunchOptionsSync();
			console.log("option",option);
			if (!this.$store.state.specialAppOnShow) {
				//#ifdef MP-WEIXIN
				this.initOnLoad();
				//#endif
				//#ifdef H5
				this.initIndex();
				//#endif
			}
		},
		onHide: function() {
			if (!this.$store.state.specialAppOnShow) {
				//取消连接
				if (!this.$validatenull(client)) {
					client.end();
				}
				//断开蓝牙连接
				if (!this.$validatenull(this.$store.state.bluetoothDevice)) {
					closeBLEConnection(this.$store.state.bluetoothDevice);
				}
				if (!this.$validatenull(this.$store.state.blueConnectTimer)) {
					clearInterval(this.$store.state.blueConnectTimer);
				}
				// if (!this.$validatenull(this.refreshTime)) {
				// 	clearInterval(this.refreshTime);
				// }
			}
		},
		methods: {
			initOnLoad() {
				let that = this;
				uni.login({
					success: (loginRes) => {
						checkRegisterAPI({
							code: loginRes.code
						}).then(([err, data]) => {
							if (err) return;
							if (data.registered == true) {
								getTokenAPI({
									oauthId: data.oauthId
								}).then(([err2, data2]) => {
									if (err2) return;
									let loginUser = {
										access_token: data2.access_token,
										avatar: data2.avatar,
										expires_in: data2.expires_in,
										nick_name: data2.nick_name,
										phone: data2.phone,
										refresh_token: data2.refresh_token,
										user_id: data2.user_id
									}
									this.$store.commit("setLoginUser",loginUser);
									//this.$u.vuex('datetime', new Date().getTime());
									refreshTokenFun();
									this.initIndex();
								})
							} else {
								this.$reLaunch("/pages/views/home/login");
							}

						})
					},
					fail:(res)=>{
						console.log("uni.login-fail",res);
						that.$showModal(res.errMsg == "login:fail Failed to fetch" ? "当前无网络信号，请稍后重试":res.errMsg);
					}
				});
			}
		}
	}
</script>
<style lang="scss">
	/*uview全局样式*/
	@import "uview-ui/index.scss";
	
	@import 'static/text_font/font.css';

	@import 'uview-ui/libs/css/flex.scss';
	/*common全局样式*/
	@import 'static/style/common.scss';
</style>
